package us.sunrisemorning.mykancolle.config;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.PropKit;
import org.apache.commons.io.IOUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jfinal.kit.LogKit;

public class GameData {
    private static final String DATA_FILE_1 = "start2.json";
    private static final String DATA_FILE_2 = "enemy.json";
    private static final String DATA_FILE_CUSTOM = "custom.json";
    
    private static JSONObject data;
    private static Map<Integer, JSONObject> shipData;
    private static Map<Integer, JSONObject> slotitemData;
    private static Map<Integer, JSONObject> slotitemEquiptypeData;
    private static Map<Integer, List<JSONObject>> mapcellData;
    private static Map<Integer, JSONObject> enemyShipData;
    private static String outputData;
    
    private GameData() {
    }

    public static void init() {
        try {
            String json = IOUtils.toString(Thread.currentThread().getContextClassLoader().getResourceAsStream(DATA_FILE_1), "UTF-8");
            data = JSON.parseObject(json);
            if (PropKit.getBoolean("useCustom")) {
                addCustomData();
            }
            initShipData(data);
            initSlotitemData(data);
            initSlotitemEquiptypeData(data);
            initMapcellData(data);
        } catch (IOException e) {
        	LogKit.error("读取主数据失败", e);
            data = new JSONObject();
        }
        String jsonStr = JSON.toJSONString(data, SerializerFeature.BrowserCompatible);
        outputData = "svdata=" + jsonStr;
        
        try {
            enemyShipData = new HashMap<>();
            String json = IOUtils.toString(Thread.currentThread().getContextClassLoader().getResourceAsStream(DATA_FILE_2), "UTF-8");
            JSONArray enemyShips = JSON.parseArray(json);
            for (int i = 0; i < enemyShips.size(); i++) {
                JSONObject ship = enemyShips.getJSONObject(i);
                enemyShipData.put(ship.getInteger("id"), ship);
            }
        } catch (IOException e) {
        	LogKit.error("读取敌舰数据失败", e);
        }
    }

    private static void addCustomData() throws IOException {
        String customjson = IOUtils.toString(Thread.currentThread().getContextClassLoader().getResourceAsStream(DATA_FILE_CUSTOM), "UTF-8");
        JSONObject customData = JSON.parseObject(customjson);
        data.getJSONObject("api_data").getJSONArray("api_mst_ship").addAll(customData.getJSONArray("api_mst_ship"));
        data.getJSONObject("api_data").getJSONArray("api_mst_shipgraph").addAll(customData.getJSONArray("api_mst_shipgraph"));
    }

    private static void initShipData(JSONObject data) {
        shipData = new HashMap<>();
        JSONArray items = data.getJSONObject("api_data").getJSONArray("api_mst_ship");
        for (int i = 0; i < items.size(); i++) {
            JSONObject item = items.getJSONObject(i);
            shipData.put(item.getInteger("api_id"), item);
        }
    }

    private static void initSlotitemData(JSONObject data) {
        slotitemData = new HashMap<>();
        JSONArray items = data.getJSONObject("api_data").getJSONArray("api_mst_slotitem");
        for (int i = 0; i < items.size(); i++) {
            JSONObject item = items.getJSONObject(i);
            slotitemData.put(item.getInteger("api_id"), item);
        }
    }

    private static void initSlotitemEquiptypeData(JSONObject data) {
        slotitemEquiptypeData = new HashMap<>();
        JSONArray items = data.getJSONObject("api_data").getJSONArray("api_mst_slotitem_equiptype");
        for (int i = 0; i < items.size(); i++) {
            JSONObject item = items.getJSONObject(i);
            slotitemEquiptypeData.put(item.getInteger("api_id"), item);
        }
    }

    private static void initMapcellData(JSONObject data) {
        mapcellData = new HashMap<>();
        JSONArray items = data.getJSONObject("api_data").getJSONArray("api_mst_mapcell");
        for (int i = 0; i < items.size(); i++) {
            JSONObject item = items.getJSONObject(i);
            List<JSONObject> mapcells = mapcellData.get(item.getInteger("api_map_no"));
            if (mapcells == null) {
                mapcells = new ArrayList<>();
                mapcellData.put(item.getInteger("api_map_no"), mapcells);
            }
            mapcells.add(item);
        }
    }

    public static JSONObject getData() {
        return data;
    }

    public static String getOutputData() {
        return outputData;
    }

    public static Map<Integer, JSONObject> getShipData() {
        return shipData;
    }

    public static Map<Integer, JSONObject> getSlotitemData() {
        return slotitemData;
    }

    public static Map<Integer, JSONObject> getSlotitemEquiptypeData() {
        return slotitemEquiptypeData;
    }
    
    public static Map<Integer, JSONObject> getEnemyShipData() {
        return enemyShipData;
    }

    // 参数为海域编号，例如1-1为11
    public static List<JSONObject> getMapcellData(int mapNo) {
        return mapcellData.get(mapNo);
    }
}
